package com.moblie.jobs;



import com.moblie.constant.RedisConstant;
import com.moblie.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/*
清理垃圾图片类
* */
public class ClearPicsJob {

    // 自定注入jedispool
    @Autowired
    private JedisPool jedisPool;

    /*
    该方法为清理七牛云服务器中和redis中的垃圾图片
    */
    public void ClearPics() {

        // 1.调用jedis对set的sdiff方法,参数为两个set集合名称,
        // 得到集合1中存在,集合2不存在的内容,并封装为一个set集合(垃圾图片的集合)
        Jedis jedis = jedisPool.getResource();
        Set<String> rubbishPics = jedis.sdiff(RedisConstant.EVALUATE_PIC_RESOURCES, RedisConstant.EVALUATE_PIC_DB_RESOURCES);
        // 2.遍历该set集合
        if (rubbishPics != null) {
            for (String rubbishPic : rubbishPics) {
                // 3.调用Qiniuutils中的deleteFileFromQiniu方法,参数为该垃圾图片名称,删除七牛云服务器中垃圾图片
                QiniuUtils.deleteFileFromQiniu(rubbishPic);
                // 4.在调用jedis的srem方法,删除reids中setmealPicResources集合中的垃圾图片
                jedis.srem(RedisConstant.EVALUATE_PIC_RESOURCES, rubbishPic);
                System.out.println("删除垃圾图片"+rubbishPic);
            }
        }
    }
}
